package com.twq.func.ctrldef

/**
 * Created by tangweiqun on 2017/11/29.
 */
object ByNameParameters {
  def main(args: Array[String]): Unit = {

    var assertionsEnabled = true

    def myAssert(predicate: () => Boolean) =
      if (assertionsEnabled && !predicate())
        throw new AssertionError

    myAssert(() => 5 > 3)

    //by-name parameter 传的是个函数
    def byNameAssert(predicate: => Boolean) =
      if (assertionsEnabled && !predicate)
        throw new AssertionError

    byNameAssert(5 > 3)


    //by-value parameter 传的是值
    def boolAssert(predicate: Boolean) =
      if (assertionsEnabled && !predicate)
        throw new AssertionError

    boolAssert(5 > 3)

    //by-name 和 by-value的区别
    val x = 5
    boolAssert(x / 0 == 0)
    byNameAssert(x / 0 == 0)
  }
}
